#include "main.h"

#define FONT_ID 2000
#define S_INDEX 0
#define L_INDEX 1


TTF_Font *Text::largeFnt = NULL, *Text::smallFnt;
SDL_Color Text::BakCol = {143, 175, 80, 0}, Text::FontCol = {0, 0, 0};
wstringstream Text::StrStream;


int Text::Init()
{
	if(-1 == TTF_Init())
		return -1;
	SDL_RWops* Font = GMUD_LoadFile(2000, L"FON");
	smallFnt = TTF_OpenFontRW(Font, 1, S_INDEX);
	if(NULL == smallFnt)
		return -2;
	return 0;
}

void Text::Shutdown()
{
	if(largeFnt)
		TTF_CloseFont(largeFnt);
	if(smallFnt)
		TTF_CloseFont(smallFnt);
	if(TTF_WasInit())
		TTF_Quit();
}

void Text::SplitStr(vector<wstring>& ss, wstring str, int width)
{
	int offset = 0, length = 1,w,h;
	ss.clear();
	while (offset + length <= str.length())
	{	
		TTF_SizeUNICODE(smallFnt, str.substr(offset, length).c_str(), &w, &h);
		if(w < width)
			++length;
		else
		{
			ss.push_back(str.substr(offset, length - 1));
			offset += length - 1;
			length = 1;
		}
	}
	if (offset < str.length())
		ss.push_back(str.substr(offset));
}

void Text::DrawOneLine(unsigned char x, unsigned char y, wstring& str)
{
	SDL_Surface* img = TTF_RenderUNICODE_Shaded(smallFnt, str.c_str(), FontCol, BakCol);
	Video::DrawImage(img, x, y);
	SDL_FreeSurface(img);
}

void Text::B_DrawOneLine(unsigned char x, unsigned char y, wstring& str)
{
	SDL_Surface* img = TTF_RenderUNICODE_Shaded(smallFnt, str.c_str(), BakCol, FontCol);
	Video::DrawImage(img, x, y);
	SDL_FreeSurface(img);
}

void Text::L_DrawOneLine(unsigned char x, unsigned char y, wstring& str)
{
	if(!largeFnt)
	{
		SDL_RWops* Font = GMUD_LoadFile(2000, L"FON");
		largeFnt = TTF_OpenFontRW(Font, 1, L_INDEX);
	}
	SDL_Surface* img = TTF_RenderUNICODE_Shaded(largeFnt, str.c_str(), FontCol, BakCol);
	Video::DrawImage(img, x, y);
	SDL_FreeSurface(img);
}